<?php
/**
 * Created by PhpStorm.
 * User: bigniu
 * Date: 2020-01-25
 * Time: 16:12:52
 */

namespace app\admin\controller;


class AdminUser extends Admin
{
    public function getList(){
        $page = input("page/i", 1) <= 1 ? 1 : input("page/i", 1);
        $info =  Db("admin")->page($page,10)->select();
        if(!$info){
            return error("暂无更多数据");
        }
        return success("成功",$info);
    }
    public function postUpdate(){
        $id=input("id");
        $password = input("password");
        if(!$password){
            return error("请输入新密码");
        }
        if(mb_strlen($password)<6){
            return error("新密码最少为6位数");
        }
        $data = [
            'password'=>adminpass($password),
        ];

        $res = Db("admin")->where(['id'=>$id])->update($data);
        if($res){
            return success("修改成功");
        }
        return error("修改失败");
    }
    public function postAdd(){
        $name = input("name");
        $password = input("password");
        $admin = Db("admin")->where(['name'=>$name])->find();
        if(!$name||!$password){
            return error("请输入用户名或密码");
        }
        if($admin){
            return error("该用户名已存在");
        }
        if(!$password){
            return error("请输入密码");
        }
        if(mb_strlen($password)<6){
            return error("密码最少为6位数");
        }
        $data = [
            'name'=>$name,
            'password'=>adminpass($password),
            'create_time'=>TIME
        ];
        $res = Db("admin")->insert($data);
        if($res){
            return success("添加成功");
        }
        return error("添加失败");
    }
    public function postDel(){
        $ids = input("ids/a");
        Db("admin")->whereIn('id', $ids)->delete();
        return success("删除成功");
    }
    public function getLogout(){
        session("admin",null);
        return success("成功");
    }
}